package com.example.binding_recyclerview.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ObservableArrayList;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;

import com.example.binding_recyclerview.R;
import com.example.binding_recyclerview.databinding.MyListItemBinding;
import com.example.binding_recyclerview.model.Student;
import com.example.binding_recyclerview.myInter.OnItemClick;

public class MyRecyclerListAdapter extends ListAdapter<Student, MyRecyclerListAdapter.MyViewHlder> {

    //视图持有者持有那个视图
    public class MyViewHlder extends RecyclerView.ViewHolder{

        MyListItemBinding listItemBinding ;

        public MyViewHlder(@NonNull View itemView) {
            super(itemView);
            listItemBinding = DataBindingUtil.bind(itemView);
        }
    }

    ObservableArrayList<Student> dataList ;

    protected MyRecyclerListAdapter(@NonNull DiffUtil.ItemCallback<Student> diffCallback) {
        super(diffCallback);
    }

    //创建视图持有者和视图
    @NonNull
    @Override
    public MyViewHlder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //通过布局映射器拿到视图
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View root = inflater.inflate(R.layout.my_list_item, parent, false);

        //通过binding拿到试图
        MyListItemBinding binding = MyListItemBinding.inflate(inflater , parent , false);
        View view = binding.getRoot();

        //绑定点击事件
        MyViewHlder myViewHlder = new MyViewHlder(root);
        myViewHlder.listItemBinding.id.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(view.getContext() , myViewHlder.listItemBinding.id.getText().toString() ,Toast.LENGTH_SHORT).show();
            }
        });

        return myViewHlder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHlder holder, int position) {
        //dataList
    }

}
